
-- --------------------------------------------------
-- Entity Designer DDL Script for SQL Server 2005, 2008, and Azure
-- --------------------------------------------------
-- Date Created: 02/02/2011 09:41:57
-- Generated from EDMX file: E:\开源项目\CodePlex\tfs.codeplex.com.tfs.TFS01\devinvest\MetadataBasedConfigArch\MetadataBasedConfigArch.Model\DBModel.edmx
-- --------------------------------------------------

SET QUOTED_IDENTIFIER OFF;
GO
USE [MDA];
GO
IF SCHEMA_ID(N'dbo') IS NULL EXECUTE(N'CREATE SCHEMA [dbo]');
GO

-- --------------------------------------------------
-- Dropping existing FOREIGN KEY constraints
-- --------------------------------------------------

IF OBJECT_ID(N'[dbo].[FK_ConfigMeta_ConfigType]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[ConfigMeta] DROP CONSTRAINT [FK_ConfigMeta_ConfigType];
GO
IF OBJECT_ID(N'[dbo].[FK_ConfigMetaConfigData_ConfigMetaConfigInfo]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[ConfigMetaConfigData] DROP CONSTRAINT [FK_ConfigMetaConfigData_ConfigMetaConfigInfo];
GO
IF OBJECT_ID(N'[dbo].[FK_ConfigMetaConfigData_Tenant]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[ConfigMetaConfigData] DROP CONSTRAINT [FK_ConfigMetaConfigData_Tenant];
GO
IF OBJECT_ID(N'[dbo].[FK_ConfigMetaConfigInfo_ConfigMeta]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[ConfigMetaConfigInfo] DROP CONSTRAINT [FK_ConfigMetaConfigInfo_ConfigMeta];
GO
IF OBJECT_ID(N'[dbo].[FK_ConfigMetaConfigInfo_Tenant]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[ConfigMetaConfigInfo] DROP CONSTRAINT [FK_ConfigMetaConfigInfo_Tenant];
GO
IF OBJECT_ID(N'[dbo].[FK_ConfigMetaConfigInfoProperty_ConfigMetaConfigInfo]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[ConfigMetaConfigInfoProperty] DROP CONSTRAINT [FK_ConfigMetaConfigInfoProperty_ConfigMetaConfigInfo];
GO
IF OBJECT_ID(N'[dbo].[FK_ConfigMetaConfigInfoProperty_Tenant]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[ConfigMetaConfigInfoProperty] DROP CONSTRAINT [FK_ConfigMetaConfigInfoProperty_Tenant];
GO
IF OBJECT_ID(N'[dbo].[FK_ConfigMetaRelations_ConfigMeta]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[ConfigMetaRelations] DROP CONSTRAINT [FK_ConfigMetaRelations_ConfigMeta];
GO
IF OBJECT_ID(N'[dbo].[FK_ConfigMetaRelations_ConfigMeta1]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[ConfigMetaRelations] DROP CONSTRAINT [FK_ConfigMetaRelations_ConfigMeta1];
GO
IF OBJECT_ID(N'[dbo].[FK_ConfigMetaRelations_ConfigMetaRelationType]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[ConfigMetaRelations] DROP CONSTRAINT [FK_ConfigMetaRelations_ConfigMetaRelationType];
GO
IF OBJECT_ID(N'[dbo].[FK_Document_Document]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Document] DROP CONSTRAINT [FK_Document_Document];
GO
IF OBJECT_ID(N'[dbo].[FK_Page_Tenant]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Page] DROP CONSTRAINT [FK_Page_Tenant];
GO
IF OBJECT_ID(N'[dbo].[FK_User_Tenant]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[User] DROP CONSTRAINT [FK_User_Tenant];
GO

-- --------------------------------------------------
-- Dropping existing tables
-- --------------------------------------------------

IF OBJECT_ID(N'[dbo].[ConfigMeta]', 'U') IS NOT NULL
    DROP TABLE [dbo].[ConfigMeta];
GO
IF OBJECT_ID(N'[dbo].[ConfigMetaConfigData]', 'U') IS NOT NULL
    DROP TABLE [dbo].[ConfigMetaConfigData];
GO
IF OBJECT_ID(N'[dbo].[ConfigMetaConfigInfo]', 'U') IS NOT NULL
    DROP TABLE [dbo].[ConfigMetaConfigInfo];
GO
IF OBJECT_ID(N'[dbo].[ConfigMetaConfigInfoProperty]', 'U') IS NOT NULL
    DROP TABLE [dbo].[ConfigMetaConfigInfoProperty];
GO
IF OBJECT_ID(N'[dbo].[ConfigMetaRelations]', 'U') IS NOT NULL
    DROP TABLE [dbo].[ConfigMetaRelations];
GO
IF OBJECT_ID(N'[dbo].[ConfigMetaRelationType]', 'U') IS NOT NULL
    DROP TABLE [dbo].[ConfigMetaRelationType];
GO
IF OBJECT_ID(N'[dbo].[ConfigType]', 'U') IS NOT NULL
    DROP TABLE [dbo].[ConfigType];
GO
IF OBJECT_ID(N'[dbo].[Document]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Document];
GO
IF OBJECT_ID(N'[dbo].[Page]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Page];
GO
IF OBJECT_ID(N'[dbo].[Tenant]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Tenant];
GO
IF OBJECT_ID(N'[dbo].[User]', 'U') IS NOT NULL
    DROP TABLE [dbo].[User];
GO

-- --------------------------------------------------
-- Creating all tables
-- --------------------------------------------------

-- Creating table 'ConfigMetas'
CREATE TABLE [dbo].[ConfigMetas] (
    [ID] bigint IDENTITY(1,1) NOT NULL,
    [ConfigType] bigint  NOT NULL,
    [Name] varbinary(50)  NOT NULL,
    [Content] varchar(50)  NOT NULL,
    [DataKey] varchar(50)  NOT NULL
);
GO

-- Creating table 'ConfigMetaConfigDatas'
CREATE TABLE [dbo].[ConfigMetaConfigDatas] (
    [ID] bigint IDENTITY(1,1) NOT NULL,
    [ConfigInfo] bigint  NOT NULL,
    [Value] varchar(50)  NOT NULL,
    [DataID] varchar(50)  NOT NULL,
    [TenantID] bigint  NOT NULL
);
GO

-- Creating table 'ConfigMetaConfigInfoes'
CREATE TABLE [dbo].[ConfigMetaConfigInfoes] (
    [ID] bigint IDENTITY(1,1) NOT NULL,
    [Name] varchar(50)  NOT NULL,
    [ConfigMeta] bigint  NOT NULL,
    [Content] varchar(50)  NOT NULL,
    [TenantID] bigint  NOT NULL
);
GO

-- Creating table 'ConfigMetaConfigInfoProperties'
CREATE TABLE [dbo].[ConfigMetaConfigInfoProperties] (
    [ID] bigint IDENTITY(1,1) NOT NULL,
    [ConfigInfo] bigint  NOT NULL,
    [Key] varchar(50)  NOT NULL,
    [Value] varbinary(50)  NOT NULL,
    [TenantID] bigint  NOT NULL
);
GO

-- Creating table 'ConfigMetaRelations'
CREATE TABLE [dbo].[ConfigMetaRelations] (
    [ID] bigint IDENTITY(1,1) NOT NULL,
    [MainConfigMeta] bigint  NOT NULL,
    [RelatedConfigMeta] bigint  NOT NULL,
    [RelationType] int  NOT NULL
);
GO

-- Creating table 'ConfigMetaRelationTypes'
CREATE TABLE [dbo].[ConfigMetaRelationTypes] (
    [ID] int IDENTITY(1,1) NOT NULL,
    [Name] varchar(50)  NOT NULL
);
GO

-- Creating table 'ConfigTypes'
CREATE TABLE [dbo].[ConfigTypes] (
    [ID] bigint IDENTITY(1,1) NOT NULL,
    [Name] varchar(50)  NOT NULL,
    [ProcessorClass] varchar(150)  NOT NULL
);
GO

-- Creating table 'Tenants'
CREATE TABLE [dbo].[Tenants] (
    [ID] bigint IDENTITY(1,1) NOT NULL,
    [Name] varchar(50)  NOT NULL
);
GO

-- Creating table 'Documents'
CREATE TABLE [dbo].[Documents] (
    [ID] bigint IDENTITY(1,1) NOT NULL,
    [Name] varchar(50)  NOT NULL,
    [Path] varchar(150)  NOT NULL,
    [TenantID] bigint  NOT NULL
);
GO

-- Creating table 'Pages'
CREATE TABLE [dbo].[Pages] (
    [ID] bigint IDENTITY(1,1) NOT NULL,
    [Name] varchar(50)  NOT NULL,
    [Content] varchar(max)  NOT NULL,
    [TenantID] bigint  NOT NULL
);
GO

-- Creating table 'Users'
CREATE TABLE [dbo].[Users] (
    [ID] bigint  NOT NULL,
    [Name] varchar(50)  NOT NULL,
    [Password] varchar(50)  NOT NULL,
    [TenantID] bigint  NOT NULL
);
GO

-- --------------------------------------------------
-- Creating all PRIMARY KEY constraints
-- --------------------------------------------------

-- Creating primary key on [ID] in table 'ConfigMetas'
ALTER TABLE [dbo].[ConfigMetas]
ADD CONSTRAINT [PK_ConfigMetas]
    PRIMARY KEY CLUSTERED ([ID] ASC);
GO

-- Creating primary key on [ID] in table 'ConfigMetaConfigDatas'
ALTER TABLE [dbo].[ConfigMetaConfigDatas]
ADD CONSTRAINT [PK_ConfigMetaConfigDatas]
    PRIMARY KEY CLUSTERED ([ID] ASC);
GO

-- Creating primary key on [ID] in table 'ConfigMetaConfigInfoes'
ALTER TABLE [dbo].[ConfigMetaConfigInfoes]
ADD CONSTRAINT [PK_ConfigMetaConfigInfoes]
    PRIMARY KEY CLUSTERED ([ID] ASC);
GO

-- Creating primary key on [ID] in table 'ConfigMetaConfigInfoProperties'
ALTER TABLE [dbo].[ConfigMetaConfigInfoProperties]
ADD CONSTRAINT [PK_ConfigMetaConfigInfoProperties]
    PRIMARY KEY CLUSTERED ([ID] ASC);
GO

-- Creating primary key on [ID] in table 'ConfigMetaRelations'
ALTER TABLE [dbo].[ConfigMetaRelations]
ADD CONSTRAINT [PK_ConfigMetaRelations]
    PRIMARY KEY CLUSTERED ([ID] ASC);
GO

-- Creating primary key on [ID] in table 'ConfigMetaRelationTypes'
ALTER TABLE [dbo].[ConfigMetaRelationTypes]
ADD CONSTRAINT [PK_ConfigMetaRelationTypes]
    PRIMARY KEY CLUSTERED ([ID] ASC);
GO

-- Creating primary key on [ID] in table 'ConfigTypes'
ALTER TABLE [dbo].[ConfigTypes]
ADD CONSTRAINT [PK_ConfigTypes]
    PRIMARY KEY CLUSTERED ([ID] ASC);
GO

-- Creating primary key on [ID] in table 'Tenants'
ALTER TABLE [dbo].[Tenants]
ADD CONSTRAINT [PK_Tenants]
    PRIMARY KEY CLUSTERED ([ID] ASC);
GO

-- Creating primary key on [ID] in table 'Documents'
ALTER TABLE [dbo].[Documents]
ADD CONSTRAINT [PK_Documents]
    PRIMARY KEY CLUSTERED ([ID] ASC);
GO

-- Creating primary key on [ID] in table 'Pages'
ALTER TABLE [dbo].[Pages]
ADD CONSTRAINT [PK_Pages]
    PRIMARY KEY CLUSTERED ([ID] ASC);
GO

-- Creating primary key on [ID] in table 'Users'
ALTER TABLE [dbo].[Users]
ADD CONSTRAINT [PK_Users]
    PRIMARY KEY CLUSTERED ([ID] ASC);
GO

-- --------------------------------------------------
-- Creating all FOREIGN KEY constraints
-- --------------------------------------------------

-- Creating foreign key on [ConfigType] in table 'ConfigMetas'
ALTER TABLE [dbo].[ConfigMetas]
ADD CONSTRAINT [FK_ConfigMeta_ConfigType]
    FOREIGN KEY ([ConfigType])
    REFERENCES [dbo].[ConfigTypes]
        ([ID])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_ConfigMeta_ConfigType'
CREATE INDEX [IX_FK_ConfigMeta_ConfigType]
ON [dbo].[ConfigMetas]
    ([ConfigType]);
GO

-- Creating foreign key on [ConfigMeta] in table 'ConfigMetaConfigInfoes'
ALTER TABLE [dbo].[ConfigMetaConfigInfoes]
ADD CONSTRAINT [FK_ConfigMetaConfigInfo_ConfigMeta]
    FOREIGN KEY ([ConfigMeta])
    REFERENCES [dbo].[ConfigMetas]
        ([ID])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_ConfigMetaConfigInfo_ConfigMeta'
CREATE INDEX [IX_FK_ConfigMetaConfigInfo_ConfigMeta]
ON [dbo].[ConfigMetaConfigInfoes]
    ([ConfigMeta]);
GO

-- Creating foreign key on [MainConfigMeta] in table 'ConfigMetaRelations'
ALTER TABLE [dbo].[ConfigMetaRelations]
ADD CONSTRAINT [FK_ConfigMetaRelations_ConfigMeta]
    FOREIGN KEY ([MainConfigMeta])
    REFERENCES [dbo].[ConfigMetas]
        ([ID])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_ConfigMetaRelations_ConfigMeta'
CREATE INDEX [IX_FK_ConfigMetaRelations_ConfigMeta]
ON [dbo].[ConfigMetaRelations]
    ([MainConfigMeta]);
GO

-- Creating foreign key on [RelatedConfigMeta] in table 'ConfigMetaRelations'
ALTER TABLE [dbo].[ConfigMetaRelations]
ADD CONSTRAINT [FK_ConfigMetaRelations_ConfigMeta1]
    FOREIGN KEY ([RelatedConfigMeta])
    REFERENCES [dbo].[ConfigMetas]
        ([ID])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_ConfigMetaRelations_ConfigMeta1'
CREATE INDEX [IX_FK_ConfigMetaRelations_ConfigMeta1]
ON [dbo].[ConfigMetaRelations]
    ([RelatedConfigMeta]);
GO

-- Creating foreign key on [ConfigInfo] in table 'ConfigMetaConfigDatas'
ALTER TABLE [dbo].[ConfigMetaConfigDatas]
ADD CONSTRAINT [FK_ConfigMetaConfigData_ConfigMetaConfigInfo]
    FOREIGN KEY ([ConfigInfo])
    REFERENCES [dbo].[ConfigMetaConfigInfoes]
        ([ID])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_ConfigMetaConfigData_ConfigMetaConfigInfo'
CREATE INDEX [IX_FK_ConfigMetaConfigData_ConfigMetaConfigInfo]
ON [dbo].[ConfigMetaConfigDatas]
    ([ConfigInfo]);
GO

-- Creating foreign key on [TenantID] in table 'ConfigMetaConfigDatas'
ALTER TABLE [dbo].[ConfigMetaConfigDatas]
ADD CONSTRAINT [FK_ConfigMetaConfigData_Tenant]
    FOREIGN KEY ([TenantID])
    REFERENCES [dbo].[Tenants]
        ([ID])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_ConfigMetaConfigData_Tenant'
CREATE INDEX [IX_FK_ConfigMetaConfigData_Tenant]
ON [dbo].[ConfigMetaConfigDatas]
    ([TenantID]);
GO

-- Creating foreign key on [TenantID] in table 'ConfigMetaConfigInfoes'
ALTER TABLE [dbo].[ConfigMetaConfigInfoes]
ADD CONSTRAINT [FK_ConfigMetaConfigInfo_Tenant]
    FOREIGN KEY ([TenantID])
    REFERENCES [dbo].[Tenants]
        ([ID])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_ConfigMetaConfigInfo_Tenant'
CREATE INDEX [IX_FK_ConfigMetaConfigInfo_Tenant]
ON [dbo].[ConfigMetaConfigInfoes]
    ([TenantID]);
GO

-- Creating foreign key on [ConfigInfo] in table 'ConfigMetaConfigInfoProperties'
ALTER TABLE [dbo].[ConfigMetaConfigInfoProperties]
ADD CONSTRAINT [FK_ConfigMetaConfigInfoProperty_ConfigMetaConfigInfo]
    FOREIGN KEY ([ConfigInfo])
    REFERENCES [dbo].[ConfigMetaConfigInfoes]
        ([ID])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_ConfigMetaConfigInfoProperty_ConfigMetaConfigInfo'
CREATE INDEX [IX_FK_ConfigMetaConfigInfoProperty_ConfigMetaConfigInfo]
ON [dbo].[ConfigMetaConfigInfoProperties]
    ([ConfigInfo]);
GO

-- Creating foreign key on [TenantID] in table 'ConfigMetaConfigInfoProperties'
ALTER TABLE [dbo].[ConfigMetaConfigInfoProperties]
ADD CONSTRAINT [FK_ConfigMetaConfigInfoProperty_Tenant]
    FOREIGN KEY ([TenantID])
    REFERENCES [dbo].[Tenants]
        ([ID])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_ConfigMetaConfigInfoProperty_Tenant'
CREATE INDEX [IX_FK_ConfigMetaConfigInfoProperty_Tenant]
ON [dbo].[ConfigMetaConfigInfoProperties]
    ([TenantID]);
GO

-- Creating foreign key on [RelationType] in table 'ConfigMetaRelations'
ALTER TABLE [dbo].[ConfigMetaRelations]
ADD CONSTRAINT [FK_ConfigMetaRelations_ConfigMetaRelationType]
    FOREIGN KEY ([RelationType])
    REFERENCES [dbo].[ConfigMetaRelationTypes]
        ([ID])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_ConfigMetaRelations_ConfigMetaRelationType'
CREATE INDEX [IX_FK_ConfigMetaRelations_ConfigMetaRelationType]
ON [dbo].[ConfigMetaRelations]
    ([RelationType]);
GO

-- Creating foreign key on [TenantID] in table 'Documents'
ALTER TABLE [dbo].[Documents]
ADD CONSTRAINT [FK_Document_Document]
    FOREIGN KEY ([TenantID])
    REFERENCES [dbo].[Tenants]
        ([ID])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_Document_Document'
CREATE INDEX [IX_FK_Document_Document]
ON [dbo].[Documents]
    ([TenantID]);
GO

-- Creating foreign key on [TenantID] in table 'Pages'
ALTER TABLE [dbo].[Pages]
ADD CONSTRAINT [FK_Page_Tenant]
    FOREIGN KEY ([TenantID])
    REFERENCES [dbo].[Tenants]
        ([ID])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_Page_Tenant'
CREATE INDEX [IX_FK_Page_Tenant]
ON [dbo].[Pages]
    ([TenantID]);
GO

-- Creating foreign key on [TenantID] in table 'Users'
ALTER TABLE [dbo].[Users]
ADD CONSTRAINT [FK_User_Tenant]
    FOREIGN KEY ([TenantID])
    REFERENCES [dbo].[Tenants]
        ([ID])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_User_Tenant'
CREATE INDEX [IX_FK_User_Tenant]
ON [dbo].[Users]
    ([TenantID]);
GO

-- --------------------------------------------------
-- Script has ended
-- --------------------------------------------------